//*****************************************************************************
//File: EmbeddedBroker.java
//This Java Source File was implemented in the same way as Prof. Alan Kaminsky
//did in the EmbeddedBroker.java class of his lecture notes.
//URL: http://www.cs.rit.edu/~ark/730/jms/jms.shtml
//The EmbeddedBroker program helps you run a embedded broker for the MQ system.
//*****************************************************************************
import org.apache.activemq.broker.BrokerService;

/**
 * Class EmbeddedBroker provides an object for running an 
 * embedded broker for the MQ system to send and receive messages.
 *
 * Usage: java EmbeddedBroker <I>host</I> <I>port</I>
 * <BR><I>host</I> = Host name of the broker service
 * <BR><I>port</I> = Port number of the broker service
 *
 * @author  Vivek Narang (vsn6751@rit.edu)
 * @author  Alan Kaminsky
 * @version 05-May-2012
 */
public final class EmbeddedBroker {

// Prevent construction of object 
    private EmbeddedBroker() {
    }

// Main Program
	/** 
	 * Main Program
	 */
    public static void main(String[] args) throws Exception {
    	
    	// Check for command line arguments
		if(args.length != 2) usage();
		
		int port;
		
		String host = args[0];
		try {
			port = Integer.parseInt(args[1]);
		
	    	BrokerService broker = new BrokerService();
	    	broker.setUseJmx(true);
	    	broker.addConnector("tcp://" + host + ":" + port);
	        broker.start();
	
	        // Now lets wait forever to avoid the JVM terminating immediately
	        Object lock = new Object();
	        synchronized (lock) {
	            lock.wait();
	        }
		}
		catch(NumberFormatException exc) {
			System.err.println("EmbeddedBroker: Invalid port: \"" +
				args[1] + "\"");
			System.exit(1);
		}
    }
    
	/**
	* Print a usage message and exit.
	 */
	private static void usage() {
		System.err.println("EmbeddedBroker Usage: java EmbeddedBroker <host> <port>");
		System.err.println("<host> is the Host name of the broker service.");
		System.err.println("<port> is the Port number of the broker service.");
		System.exit(1);
	}
}